package com.wn.web;

import com.github.pagehelper.PageInfo;
import com.wn.common.annotation.RequireRoles;
import com.wn.common.pojo.Order;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.service.OrderService;
import com.wn.service.PayService;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.function.ServerResponse;
import sun.rmi.runtime.Log;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestControllerAdvice
@RequestMapping("order")
@ApiModel(value="订单接口",description = "订单接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private PayService payService;



    /**
     * 添加订单接口
     * @param order
     * @return
     */
    @PostMapping("insert")
    @RequireRoles(roles = "super_admin",index = 1,type=Order.class)
    public Rsp insertOrder(@RequestBody @Valid Order order,@RequestHeader("token")String token){
       return orderService.insertOrder(order);

    }


    /**
     * 删除接口
     * @param order
     * @return
     */

    @PostMapping("delete")
    @RequireRoles(roles = "super_admin",index=1,type = Order.class)
    public Rsp deleteOrder(@RequestBody Order order,@RequestHeader("token")String token){
        return orderService.deleteOrder(order.getId(),order.getUpdateUserId(),order.getUpdateTime());

    }

    /**
     * 根据主键查询订单
     * @param orderId
     * @return
     */
    @PostMapping("queryByPrimaryKey/{orderId}")
    public Rsp queryOrderByPrimaryKey(@PathVariable("orderId") Integer orderId){
        return orderService.getOrderById(orderId);

    }

    /**
     * 按条件查询订单
     * @param pageParam
     * @return
     */

    @PostMapping("queryByCondition")
    @RequireRoles(roles="super_admin",index=0,type=Order.class)
    public Rsp queryOrderListByCondition(@RequestHeader("tolen")String token,@RequestBody PageParam<Order> pageParam){
       return  orderService.orderpageInfo(pageParam.getPageNum(),pageParam.getPageSize(),pageParam.getModel());

    }

    /**
     * 查询全部订单列表
     * @param pageParam
     * @return
     */
    @PostMapping("queryAll")
    @RequireRoles(roles = "super_admin",index=0,type = Order.class)
    public Rsp queryOrderList(@RequestHeader("token")String token,@RequestBody PageParam<Order> pageParam){
       return orderService.orderpageInfo(pageParam.getPageNum(),pageParam.getPageSize(),null);

    }

    @PostMapping("queryOrderStatus")
    public Rsp queryOrderStatus(@RequestBody Order order){
        log.info("要查询的支付编号{}",order.getOrderPaynumber());
        //查询订单状态  如果状态返回是1则支付成功  res.data.data.orderStatus
        return payService.queryOrderStatusFromDB(order.getOrderPaynumber());
    }

    @PostMapping("update")
    @RequireRoles(roles = "{super_admin}",index=1,type = Order.class)
    public Rsp updateOrder(@RequestBody Order order,@RequestHeader("token")String token){
       return  orderService.updateOrderInformation(order);
    }
    @GetMapping("updateOrderStatus")
    public Rsp updateOrderStatus(String orderNo){

        return orderService.updateOrder(orderNo,new Date());
    }

    @PostMapping("createOrder")
    @RequireRoles(roles = {"super_admin","admin","member"},type = Order.class,index = 1)
    public Rsp createOrder(@RequestBody Order order,@RequestHeader String token){
        log.info("添加的订单信息{}",order);

        return payService.createOrder(order);
    }



}

